package com.framework.tomcat.ex03.pyrmont.connector.http;

import com.framework.tomcat.ex03.pyrmont.processor.HttpProcessor;
import org.apache.catalina.Context;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author LIN
 * @date 2022-06-13 22:52
 */
public class HttpConnector implements Runnable{

    boolean stop = false;
    

    private String scheme = "http";
    private Context context;

    public String getScheme(){
        return this.scheme;
    }
    @Override
    public void run() {
        ServerSocket serverSocket = null;
        int port = 8080;
        try {
            serverSocket = new ServerSocket(port,1, InetAddress.getByName("127.0.0.1"));

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        while(!stop){
            Socket accept = null;
            try{
                accept = serverSocket.accept();
            }catch(Exception e){
                System.err.println(e.getMessage());
                continue;
            }

            HttpProcessor httpProcessor = new HttpProcessor(this);
            httpProcessor.process(accept);


        }
    }

    public void start(){
        Thread thread = new Thread(this);
        thread.start();
    }

    public void setContainer(Context context) {
        this.context = context;
    }

    public void initialize() {
    }
}
